package logic

import (
	"context"
	"net/http"
	"poblish_house/api/internal/svc"
	"poblish_house/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ImageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewImageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ImageLogic {
	return &ImageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ImageLogic) Image(r *http.Request) (resp *types.Response, err error) {
	file, h, err := r.FormFile("image")
	if err != nil {
		return &types.Response{
			Code: -1,
			Msg:  "上传失败",
			Data: nil,
		}, nil
	}
	file.Close()
	if h.Size > 1024*1024*2 {
		return &types.Response{
			Code: -2,
			Msg:  "上传过大",
			Data: nil,
		}, nil
	}
	if h.Filename != "jpg" && h.Filename != "png" {
		return &types.Response{
			Code: -3,
			Msg:  "上传格式错误",
			Data: nil,
		}, nil
	}

	return &types.Response{
		Code: 0,
		Msg:  "上传成功",
		Data: nil,
	}, nil
}
